package com.test.rpc.provider;

import com.test.rpc.ComputeEntity;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

@Slf4j
public class RpcApplication {

    public static void main(String[] args) {
        try {
            // 初始化服务端socket并且绑定9090端口
            ServerSocket serverSocket = new ServerSocket(9090);
            log.info("socket port {}", serverSocket.getLocalSocketAddress());
            //等待客户端的连接
            Socket socket = serverSocket.accept();

            //获取输入流
            ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
            //通过反序列化创建对象
            ComputeEntity request = (ComputeEntity) inputStream.readObject();
            Compute compute = new Compute();
            int result = compute.add(request.getA(), request.getB());

            ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
            log.info("server compute result is {}", result);
            outputStream.writeObject(result);

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

}
